package com.imessage.action.base.config;


import com.bean.BaseConfig;
import com.imessage.action.BaseAction;
import com.imessage.type.BaseConfigCode;
import com.imessage.type.BaseState;
import com.imessage.vo.ConfigVo;
import io.ebean.Expr;
import io.ebean.Expression;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

public class ConfigBaseAction extends BaseAction {

    protected final String datagrid="datagrid-baseConfig-filter";

    protected void setExpressionList(List<Expression> expressionList, ConfigVo configVo){
        if(StringUtils.isNotBlank(configVo.getName())){
            expressionList.add(Expr.contains("name", configVo.getName()));
        }
        if(StringUtils.isNotBlank(configVo.getCode())){
            expressionList.add(Expr.eq("code", configVo.getCode()));
        }
    }

    protected boolean existConfig(String code,Long base_config_id){
        List<Expression> expressionList=new ArrayList<Expression>();
        if(StringUtils.isNotBlank(code)){
            expressionList.add(Expr.eq("code", code));
        }
        if(base_config_id!=null){
            expressionList.add(Expr.ne("id", base_config_id));
        }
        BaseConfig baseConfig =baseService.findObject(BaseConfig.class,expressionList);
        if(baseConfig==null){
            return false;
        }
        return true;
    }

    protected void setModelAndView(ModelAndView modelAndView,BaseConfig baseConfig){
        modelAndView.addObject("baseConfigForm", baseConfig);
        modelAndView.addObject("statusList", BaseState.values());
        modelAndView.addObject("codesList", BaseConfigCode.values());
    }

    protected void setBaseConfig(ConfigVo configVo,BaseConfig baseConfig){
        copyProperties(configVo, baseConfig);
        if(StringUtils.isNotBlank(configVo.getState())){
            baseConfig.setState(BaseState.fromValue(configVo.getState()));
        }
    }

}
